package com.yzt.dynamicmodule.dialog;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yzt.dynamicmodule.R;

import com.yzt.p_base.utils.ToastUtil;
import com.yzt.plugin_imageloader.ImageLoadUtil;
import com.yzt.plugin_network.response.HttpResponse;
import com.yzt.plugin_widget.RoundImageView;
import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.dialog.BaseDialog;
import com.yzt.zhuju.dialog.BottomBaseDialog;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.dynamic.DynamicListBean;
import com.yzt.zhuju.network.entity.user.FollowUserStatusRequest;
import com.yzt.zhuju.utils.FastClickUtils;
import com.yzt.zhuju.utils.FormatUtils;
import com.yzt.zhuju.widget.tuiview.TUITextView;

import java.util.ArrayList;
import java.util.List;

/**
 * author : bob
 * date   : 2023/2/27
 * desc   : 动态详情
 */
@SuppressLint("SetTextI18n")
public class DynamicDetailDialog extends BottomBaseDialog {

    private DynamicListBean detail;

    String dynamicId;

    private RoundImageView img_header;
    private TextView tv_user_name;
    private TUITextView tv_follow;

    private TextView tv_title;
    private TextView tv_content;
    private TextView tv_time;

    private TUITextView tv_like;
    private TUITextView tv_msg;
    private TUITextView tv_share;

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_dynamic_detail;
    }

    @Override
    public void bindView(View v) {
        dynamicId = getArguments().getString("extra_dynamic_id");

        img_header = findViewById(R.id.img_header);
        tv_user_name = findViewById(R.id.tv_user_name);
        tv_follow = findViewById(R.id.tv_follow);

        tv_title = findViewById(R.id.tv_title);
        tv_content = findViewById(R.id.tv_content);
        tv_time = findViewById(R.id.tv_time);

        tv_like = findViewById(R.id.tv_like);
        tv_msg = findViewById(R.id.tv_msg);
        tv_share = findViewById(R.id.tv_share);

        findViewById(R.id.tv_follow).setOnClickListener(this);
        findViewById(R.id.tv_like).setOnClickListener(this);
        findViewById(R.id.tv_msg).setOnClickListener(this);
        findViewById(R.id.tv_share).setOnClickListener(this);
        findViewById(R.id.tv_message).setOnClickListener(this);

        // 获取动态详情
        getDetails();
    }

    private void getDetails() {
        BizLayer.getInstance().getDynamicModule().dynamicDetail(dynamicId, new AresSubscriber<DynamicListBean>() {
            @Override
            protected void onSuccess(DynamicListBean response) {
                detail = response;
                loadLocalData();
            }
        });
    }

    /**
     * 加载页面数据
     */
    private void loadLocalData() {

        String userId = RXingUtils.getPersonDetail().getBasic().getUserId();

        // 用户信息
        ImageLoadUtil.loadAvatar(mActivity, detail.getAvatar(), img_header);
        tv_user_name.setText(detail.getNickName());

        // 关注
        if (userId.equals(detail.getUserId() + "")) {
            tv_follow.setVisibility(View.GONE);
        } else {
            tv_follow.setVisibility(View.VISIBLE);
            tv_follow.setSelected(detail.isFollowed());
        }

        // 动态内容
        tv_time.setText("我是title?");
        tv_content.setText(detail.getContent());

        // 地址
        if (detail.getLocation() != null) {
            if (detail.getDistance() > 1000) {
                tv_time.setText(detail.getLocation() + " · " + FormatUtils.formatKeepOne(detail.getDistance() / 1000) + "km");
            } else {
                tv_time.setText(detail.getLocation() + " · " + detail.getDistance() + "m");
            }
        } else {
            tv_time.setText("");
        }

        // 点赞数量
        tv_like.setSelected(detail.isLiked());
        tv_like.setText(String.valueOf(detail.getLikeCount()));

        // 评论数量
        tv_msg.setText(String.valueOf(detail.getCommentCount()));

        // 分享数量
        tv_share.setText(String.valueOf(0));
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);

        if (FastClickUtils.isFastClick()) {
            return;
        }

        if (detail == null) {
            return;
        }

//        // 更多
//        if (view.getId() == R.id.img_more) {
//            MenuSelectDialog menuSelectDialog = new MenuSelectDialog(mActivity, R.style.ActionSheetDialogStyle);
//
//            String btn;
//
//            String userId = RXingUtils.getPersonDetail().getBasic().getUserId();
//
//            if (userId.equals(detail.getUserId() + "")) {
//                btn = getString(R.string.delete);
//            } else {
//                btn = getString(R.string.report);
//            }
//
//            menuSelectDialog.setLogoutText(btn);
//
//            menuSelectDialog.setOnItemClickListener(i1 -> {
//                if (i1 == 1) {
//                    if (!userId.equals(detail.getUserId() + "")) {
//                        // 举报
//                        RouterAccount.ReportActivity(detail.getUserId() + "", dynamicId);
//                        return;
//                    }
//
//                    // 删除
//                    AppDialogUtil.showTitleDialog(mActivity, "", getString(R.string.del_dynamic), mActivity.getString(R.string.cancel), mActivity.getString(R.string.confirm), new AppDialogUtil.DialogClickInterface() {
//                        @Override
//                        public void onCancelClick() {
//
//                        }
//
//                        @Override
//                        public void onSureClick() {
//                            BizLayer.getInstance().getDynamicModule().dynamicDel(dynamicId, new AresSubscriber<HttpResponse>() {
//                                @Override
//                                protected void onSuccess(HttpResponse httpResponse) {
//                                    ToastUtil.showMessage(getString(R.string.dynamic_del_success));
//                                    RxBus.getDefault().post(new RefreshDynamicEvent());
//                                    dismissAllowingStateLoss();
//                                }
//                            });
//                        }
//                    });
//                }
//            });
//        }
//
//        // 用户信息
//        if (view.getId() == R.id.ll_user_info) {
//            RouterUser.PersonalDetailActivity(detail.getUserId() + "", "动态");
//        }

        // 关注
        if (view.getId() == R.id.tv_follow) {
            FollowUserStatusRequest mFollowRequest = new FollowUserStatusRequest();
            List<String> list = new ArrayList<>();
            list.add(detail.getUserId() + "");
            mFollowRequest.setUidRels(list);
            mFollowRequest.setStatus(1);
            BizLayer.getInstance().getAccountModule().postFollowOrUnfollowUser(mFollowRequest, new AresSubscriber<HttpResponse>() {
                @Override
                protected void onSuccess(HttpResponse response) {
                    ToastUtil.showMessage(getString(R.string.user_follow_success));
                    tv_follow.setVisibility(View.GONE);
                }
            });
        }

        // 点赞
        if (view.getId() == R.id.tv_like) {
            if (tv_like.isSelected()) {
                BizLayer.getInstance().getDynamicModule().dynamicUnLike(dynamicId, new AresSubscriber<HttpResponse>() {
                    @Override
                    protected void onSuccess(HttpResponse response) {
                        detail.setLiked(false);
                        tv_like.setSelected(false);
                        detail.setLikeCount(detail.getLikeCount() - 1);
                        ToastUtil.showMessage(getString(R.string.touch_like_uncheck));
                        tv_like.setText(String.valueOf(detail.getLikeCount()));
                    }
                });
            } else {
                BizLayer.getInstance().getDynamicModule().dynamicLike(dynamicId, new AresSubscriber<HttpResponse>() {
                    @Override
                    protected void onSuccess(HttpResponse response) {
                        detail.setLiked(true);
                        tv_like.setSelected(true);
                        detail.setLikeCount(detail.getLikeCount() + 1);
                        ToastUtil.showMessage(getString(R.string.touch_like_check));
                        tv_like.setText(String.valueOf(detail.getLikeCount()));
                    }
                });
            }
        }

        // 分享
        if (view.getId() == R.id.tv_share) {
            IndexShareDialog share = (IndexShareDialog) BaseDialog.showDialog(mActivity, IndexShareDialog.class);
            share.setListener(new IndexShareDialog.OnSelectOnClickListener() {
                @Override
                public void onShare(int type) {
                    ToastUtil.showMessage("分享");
                }
            });
        }

        // 评论
        if (view.getId() == R.id.tv_msg || view.getId() == R.id.tv_message) {
            // 评论
            Bundle extras = new Bundle();
            extras.putString("extra_dynamic_id", dynamicId);
            DynamicCommentDialog.showDialog(mActivity, DynamicCommentDialog.class, extras);
        }
    }
}